package com.midea.ai.air.ui.home.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.midea.ai.air.R;
import com.midea.ai.air.common.ActionBarBackFragment;
import com.midea.ai.air.common.BaseFragment;
import com.midea.ai.air.common.Constant;
import com.midea.ai.air.ui.home.MainActivityNew;
import com.midea.ai.air.ui.home.data.DragPage;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ouyangdi on 2016/4/20.
 * 主页定制Fragment
 */
public class HomeSettingFragment extends BaseFragment {
    public static final int LISTVIEW_TYPE_COUNT = 2;
    public static final int LISTVIEW_TYPE_EMPTY = 0;
    public static final int LISTVIEW_TYPE_ITEM = 1;
    public static final int LISTVIEW_TYPE_ITEM_IN = 2;
    public static final int LISTVIEW_TYPE_ITEM_OUT = 3;

    ActionBarBackFragment mActionBar;

    private LinearLayout page_linearLayout;
    private List<String> mListIn, mListOut;
    private ListView mListView;
    private ListView mListView_Drag;

    MainActivityNew mActivityNew;

    private static List<DragPage> mList_Page = new ArrayList<>();
    public static String tag_A = "A";
    public static String tag_B = "B";
    public static String tag_C = "C";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_page_setting, container, false);
        initPageData();
        initView(view);
        return view;
    }


    /**
     * 初始化主页定制数据
     */
    private void initPageData() {
        mList_Page.clear();
        mList_Page.add(new DragPage(tag_A, getString(R.string.air_home_page_setting_room)));
        mList_Page.add(new DragPage(tag_A, getString(R.string.air_device_ac)));
        mList_Page.add(new DragPage(tag_A, getString(R.string.air_device_pur)));
        mList_Page.add(new DragPage(tag_A, getString(R.string.power_home)));
        mList_Page.add(new DragPage(tag_A, getString(R.string.air_main_home_test)));
        mList_Page.add(new DragPage(tag_A, getString(R.string.air_home)));
        mList_Page.add(new DragPage(tag_A, getString(R.string.advice_home)));

        mList_Page.add(new DragPage(tag_C, "不包括"));

        mList_Page.add(new DragPage(tag_B, getString(R.string.air_device_hum)));
        mList_Page.add(new DragPage(tag_B, getString(R.string.air_device_dehum)));

    }


    /**
     * 初始化数据
     */
    private void initData() {
        if (mListIn == null) {
            mListIn = new ArrayList<>();
        }
        mListIn.add(getString(R.string.air_home_page_setting_room));
        mListIn.add(getString(R.string.nav_bar_device));
        mListIn.add(getString(R.string.power_home));

        mListIn.add(getString(R.string.advice_home));

        if (mListOut == null) {
            mListOut = new ArrayList<>();
        }
        mListOut.add(getString(R.string.check_home));
        mListOut.add(getString(R.string.air_home));

        mActivityNew = (MainActivityNew) getActivity();
    }


    private void initView(View view) {
        initActionBar(view);
        mActivityNew = (MainActivityNew) getActivity();


        mListView_Drag = (ListView) view.findViewById(R.id.list_home_page_drag);
        mListView_Drag.setAdapter(new DragListAdapter(getActivity(), mList_Page));

    }



    private void initActionBar(View view) {
        FrameLayout mBarLayout = (FrameLayout) view.findViewById(R.id.bar_parent);
        ViewGroup.LayoutParams LP = mBarLayout.getLayoutParams();
        LP.height = Constant.getTopMargin() + getResources().getDimensionPixelSize(R.dimen.height_44dp);
        mBarLayout.setLayoutParams(LP);
        mActionBar = (ActionBarBackFragment) getChildFragmentManager().findFragmentById(R.id.bar_fragment_home_setting);
        mActionBar.getBarFullLayout().setBackgroundColor(Color.TRANSPARENT);

        TextView actionBarRightBtn = mActionBar.getTvBarRight();
        actionBarRightBtn.setText(getString(R.string.accomplish));
        actionBarRightBtn.setVisibility(View.VISIBLE);
        actionBarRightBtn.setTextColor(getResources().getColor(R.color.btn_white_text_selector));
        actionBarRightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActivityNew.startOrStopPageSet(false);
            }
        });

        TextView actionBarLeftBtn = mActionBar.getTvBarLeft();
        mActionBar.getBackButton().setVisibility(View.GONE);
        actionBarLeftBtn.setVisibility(View.VISIBLE);
        actionBarLeftBtn.setTextColor(getResources().getColor(R.color.btn_white_text_selector));
        actionBarLeftBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActivityNew.startOrStopPageSet(false);
            }
        });

        mActionBar.getTvTitle().setText(getString(R.string.air_home_page_setting));
        mActionBar.getTvTitle().setTextColor(getResources().getColor(R.color.common_white));
        mActionBar.getBotLine().setVisibility(View.INVISIBLE);
    }


    public static class DragListAdapter extends ArrayAdapter<DragPage> {
        public DragListAdapter(Context context, List<DragPage> objects) {
            super(context, 0, objects);
        }

        public List<DragPage> getList() {
            return mList_Page;
        }

        int pos = 0;

        // 利用模板布局不同的listview
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view = convertView;
            final DragPage item = getItem(position);
            if (tag_C.equals(getItem(position).getTag())) {
                // 标题组
                view = LayoutInflater.from(getContext()).inflate(R.layout.item_empty_home_page_list, null);
                pos = position;
            } else {
                view = LayoutInflater.from(getContext()).inflate(R.layout.item_home_setting_list, null);
                ImageView img = (ImageView) view.findViewById(R.id.iv_delete_add);
                TextView textView = (TextView) view.findViewById(R.id.tv_module_name);
                textView.setText(getItem(position).getContent());

                if (item.getTag().equals(tag_A)) {
                    img.setImageResource(R.drawable.air_home_page_setting_delete);
                } else {
                    img.setImageResource(R.drawable.air_home_page_setting_add);
                }

                img.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
//                        Log.e("position", "被点击图片的位置：" + position);
                        if (item.getTag().equals(tag_A)) {
                            item.setTag(tag_B);
                            mList_Page.remove(position);
                            mList_Page.add(pos, item);
                            item.setTag(tag_B);
                        } else {
                            item.setTag(tag_A);
                            mList_Page.remove(position);
                            if (pos == 0) {
                                mList_Page.add(0, item);
                            } else {
                                mList_Page.add(pos, item);
                            }
                            item.setTag(tag_A);
                        }
                        notifyDataSetChanged();
                    }
                });
            }
            return view;
        }
    }
}
